package com.google.code.p.slideunlocker;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class SleepListenService extends Service{
    public class LocalBinder extends Binder {
        SleepListenService getService() {
            return SleepListenService.this;
        }
    }
    @Override
    public IBinder onBind(Intent intent) {
        return mBinder;
    }

    // This is the object that receives interactions from clients.  See
    // RemoteService for a more complete example.
    private final IBinder mBinder = new LocalBinder();
    private BroadcastReceiver screenoffReceiver;
    private NotificationManager nm;

    private void registerIntentReceivers() {
      IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
      registerReceiver(screenoffReceiver, filter);
    }
    
    @Override
    public void onStart(Intent intent, int startId) {
        
        super.onStart(intent, startId);
    }
    
    @Override
    public void onCreate() {
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Notification notification = 
          new Notification(R.drawable.icon, "SlideUnlocker background service started.", System.currentTimeMillis());
        notification.setLatestEventInfo(this, "SlideUnlocker background service started.", 
            "Use SlideUnlocker's menu option to stop this service.",
            PendingIntent.getActivity(this, 0,
                new Intent(this, SlideUnlocker.class), 0)
            );
        nm.notify(R.layout.main, notification);
        screenoffReceiver = new BroadcastReceiver(){

            @Override
            public void onReceive(Context context, Intent intent) {
                Intent i = new Intent(SleepListenService.this, SlideUnlocker.class);
                i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                PendingIntent pi = PendingIntent.getActivity(SleepListenService.this, 0, i, 0);
                try {
                  pi.send();
                } catch (CanceledException e) {
                  Log.e(getClass().toString(), e.getMessage(), e);
                }
            }
            
        };
        
        registerIntentReceivers();
    }
    
    @Override
    public void onDestroy() {
      Notification notification = 
        new Notification(R.drawable.icon, "SlideUnlocker background service stopped.", System.currentTimeMillis());
      notification.setLatestEventInfo(this, "SlideUnlocker background service stopped.", 
          "Starting SlideUnlocker will automatically restart this service.",
          PendingIntent.getActivity(this, 0,
              new Intent(this, SlideUnlocker.class), 0)
          );
      nm.notify(R.layout.main, notification);
      if(screenoffReceiver != null){
        unregisterReceiver(screenoffReceiver);
      }
    }

}